package com.mall.quartz.controller;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.StringUtils;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.quartz.domain.SysJob;
import com.mall.quartz.domain.SysJobLog;
import com.mall.quartz.service.ISysJobLogService;
import com.mall.quartz.service.ISysJobService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 调度日志操作处理
 * Created by zhonglin on 2020/4/8.
 */
@Controller
@RequestMapping("/monitor/jobLog")
public class SysJobLogController
    extends BaseController {

  private String prefix = "monitor/job";

  @Autowired
  private ISysJobService jobService;

  @Autowired
  private ISysJobLogService jobLogService;

  @RequiresPermissions("monitor:job:view")
  @GetMapping()
  public String jobLog(@RequestParam(value = "jobId", required = false) Long jobId,
                       ModelMap mmap) {

    if (StringUtils.isNotNull(jobId)) {
      SysJob job = jobService.selectJobById(jobId);
      mmap.put("job", job);
    }
    return prefix + "/jobLog";
  }

  @RequiresPermissions("monitor:job:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(SysJobLog jobLog) {

    startPage();
    List<SysJobLog> list = jobLogService.selectJobLogList(jobLog);
    return getDataTable(list);
  }

  @Log(title = "调度日志", businessType = BusinessType.EXPORT)
  @RequiresPermissions("monitor:job:export")
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(SysJobLog jobLog) {

    List<SysJobLog>      list = jobLogService.selectJobLogList(jobLog);
    ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class);
    return util.exportExcel(list, "调度日志");
  }

  @Log(title = "调度日志", businessType = BusinessType.DELETE)
  @RequiresPermissions("monitor:job:remove")
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(jobLogService.deleteJobLogByIds(ids));
  }

  @RequiresPermissions("monitor:job:detail")
  @GetMapping("/detail/{jobLogId}")
  public String detail(@PathVariable("jobLogId") Long jobLogId,
                       ModelMap mmap) {

    mmap.put("name", "jobLog");
    mmap.put("jobLog", jobLogService.selectJobLogById(jobLogId));
    return prefix + "/detail";
  }

  @Log(title = "调度日志", businessType = BusinessType.CLEAN)
  @RequiresPermissions("monitor:job:remove")
  @PostMapping("/clean")
  @ResponseBody
  public AjaxResult clean() {

    jobLogService.cleanJobLog();
    return success();
  }

}
